package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.NavModel;
import com.zeng.jiudian.bean.NavModelExample;
import com.zeng.jiudian.bean.NavModelExample.Criteria;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.dao.NavDao;
import com.zeng.jiudian.mapper.NavMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 16:39
 */
public class NavDaoImpl implements NavDao
{
    private SqlSession sqlSession;

    @Override
    public int add(NavModel navModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        navModel.setCreateTime(dateFormat.format(new Date()));
        navModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NavMapper mapper = sqlSession.getMapper(NavMapper.class);
            count = mapper.insert(navModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<NavModel> findAll(NavModel navModel)
    {
        List<NavModel> list = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NavMapper mapper = sqlSession.getMapper(NavMapper.class);
            NavModelExample example = new NavModelExample();
            Criteria criteria = example.createCriteria();

            if (!"".equals(navModel.getNavName()))
            {
                criteria.andNavNameLike("%" + navModel.getNavName() + "%");
            }
            if (!"".equals(navModel.getNavHref()))
            {
                criteria.andNavHrefLike("%" + navModel.getNavHref() + "%");
            }
            if (navModel.getSort() != (-1))
            {
                criteria.andSortEqualTo(navModel.getSort());
            }
            if (navModel.getIsShow() != null && navModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(navModel.getIsShow());
            }
            if (navModel.getRecommend() != null && navModel.getRecommend() >= 0)
            {
                criteria.andIsShowEqualTo(navModel.getRecommend());
            }

            if (!"无".equals(navModel.getOrder()))
            {
                if ("创建时间".equals(navModel.getOrder()))
                {
                    if ("降序".equals(navModel.getLift()))
                    {
                        example.setOrderByClause("create_time desc");
                    }
                    else if ("升序".equals(navModel.getLift()))
                    {
                        example.setOrderByClause("create_time asc");
                    }
                }
                else if ("更新时间".equals(navModel.getOrder()))
                {
                    if ("降序".equals(navModel.getLift()))
                    {
                        example.setOrderByClause("update_time desc");
                    }
                    else if ("升序".equals(navModel.getLift()))
                    {
                        example.setOrderByClause("update_time asc");
                    }
                }
            }
            else
            {
                if ("降序".equals(navModel.getLift()))
                {
                    example.setOrderByClause("id desc");
                }
                else if ("升序".equals(navModel.getLift()))
                {
                    example.setOrderByClause("id asc");
                }
            }
            PageModel pageModel = navModel.getPageModel();
            if (pageModel != null)
            {
                Integer nowNum = pageModel.getNowNum();
                Integer pageSize = pageModel.getPageSize();
                if (nowNum != null && pageSize != 0 && pageSize != null)
                {
                    example.setStartRow((nowNum - 1) * pageSize);
                    example.setPageSize(pageSize);
                }
            }
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int getCount(NavModel navModel)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NavMapper mapper = sqlSession.getMapper(NavMapper.class);
            NavModelExample example = new NavModelExample();
            Criteria criteria = example.createCriteria();

            if (!"".equals(navModel.getNavName()))
            {
                criteria.andNavNameLike("%" + navModel.getNavName() + "%");
            }
            if (!"".equals(navModel.getNavHref()))
            {
                criteria.andNavHrefLike("%" + navModel.getNavHref() + "%");
            }
            if (navModel.getSort() != (-1))
            {
                criteria.andSortEqualTo(navModel.getSort());
            }
            if (navModel.getIsShow() != null && navModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(navModel.getIsShow());
            }
            if (navModel.getRecommend() != null && navModel.getRecommend() >= 0)
            {
                criteria.andIsShowEqualTo(navModel.getRecommend());
            }

            count = (int) mapper.countByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int delete(int id)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NavMapper mapper = sqlSession.getMapper(NavMapper.class);
            count = mapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public NavModel findById(int id)
    {
        NavModel model = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NavMapper mapper = sqlSession.getMapper(NavMapper.class);
            model = mapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return model;
    }

    @Override
    public int update(NavModel navModel)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NavMapper mapper = sqlSession.getMapper(NavMapper.class);
            count = mapper.updateByPrimaryKeySelective(navModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<NavModel> getAll()
    {
        List<NavModel> list = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            NavMapper mapper = sqlSession.getMapper(NavMapper.class);
            NavModelExample example = new NavModelExample();
            Criteria criteria = example.createCriteria();
            criteria.andIsShowEqualTo(1);
            example.setOrderByClause("sort asc");
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }
}
